var path = require('path');
var utils = require('./utils');
var build = require('../../build-number');
var lodash = require('lodash');
var storeId = process.env.mpjStoreId || '4zbnll74v8g0skos4o8s4ks0w';
var defaultDataRootPath = path.normalize(process.cwd() + '/data/' + storeId);
var defaultLogRootPath = path.normalize(process.cwd() + '/logs/' + storeId);

utils.mkdirsSync(defaultDataRootPath);
utils.mkdirsSync(defaultLogRootPath);

var webconfig = require("../../../../../webconfig").webconfig;
if(process.env.mpjOptions && typeof(JSON.parse(process.env.mpjOptions)) == "string"){
   process.env.mpjOptions = JSON.parse(process.env.mpjOptions);
}

export var config: any = {
    version: build,
    storeId: storeId,
    options: process.env.mpjOptions ? JSON.parse(process.env.mpjOptions) : {},
    checkResourceCount: true,
    serverRootURL: webconfig.mpjServerRootURL,
    dataRootDir: defaultDataRootPath,
    logRootDir: defaultLogRootPath,
    heartBeatSchedule: 1 * 60 * 1000,
    type2spUrl: {
        'sync-packages': 'sync-packages',
        'devices': 'devices',
        'checkout': 'checkouts',
        'deskTable': 'tables',
        'tableParty': 'tableParties',
        'orders': 'orders',
        'preOrders': 'preorders',
        'soldOut': 'inventories/soldOut'
    },
    spUrl2Type: {
        'sync-packages': 'sync-packages',
        'devices': 'devices',
        'tables': 'deskTable',
        'orders': 'orders',
        'preorders': 'preOrders',
        'inventories/soldOut': 'soldOut'
    },
    dataStores: {
        type: 'common/nedb-storage',
        resources: {
            options: {
                options: {
                    filename: defaultDataRootPath + '/resources.dat',
                    inMemoryOnly: false,
                    autoload: true,
                    onSave: function (doc) {
                        if (doc.timeWhen == 'DBTimestamp')
                            doc.timeWhen = new Date().getTime();
                    }
                },
                index: [{
                    fieldName: 'id',
                    unique: true
                }, {
                    fieldName: 'timeWhen',
                    unique: false
                }]
            }
        },
        updates: {
            options: {
                options: {
                    filename: defaultDataRootPath + '/updates.dat',
                    inMemoryOnly: false,
                    autoload: true,
                    autocompactionInterval: 120 * 60 * 1000
                }
            }
        },
        uploads: {
            options: {
                options: {
                    filename: defaultDataRootPath + '/uploads.dat',
                    inMemoryOnly: false,
                    autoload: true,
                    autocompactionInterval: 120 * 60 * 1000
                },
                index: [{
                    fieldName: 'id',
                    unique: false
                }, {
                    fieldName: 'version',
                    unique: false
                }, {
                    fieldName: 'packId',
                    unique: false
                }]
            }
        }
    }
};
config.fbs = config.options.option && config.options.option.info && config.options.option.info.services && config.options.option.info.services.fbs;
config.ldc = lodash.get(config, 'options.ldc', true);